package cn.com.forever.offer;

//青蛙跳台阶  一次性跳1阶或2阶
public class JumpFloor {
    public static void main(String[] args) {
        System.out.println(jumpfloor(5));
    }

    //递归解法
    public static int jumpfloor(int n){
        if(n <= 0){
            return -1;
        }
        if(n == 1){
            return 1;
        }
        if(n == 2){
            return 2;
        }
        return jumpfloor(n-1)+jumpfloor(n-2);
    }

    //循环解法
    public static int jumpfloor2(int target){
        if(target <= 0){
            return -1;
        }
        if(target == 1){
            return 1;
        }
        if(target == 2){
            return 2;
        }
        int first = 1;
        int second = 2;
        int ans = 0;
        for (int i = 3; i <= target; i++) {
            ans = first + second;
            first = second;
            second = ans;
        }
        return ans;
    }
}
